package com.cb.mapper;
import com.cb.domain.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;


public interface UserMapper{
    //登录  根据邮箱和密码
    @Select("select * from user where user_email=#{email} AND user_password=#{password}")
    @Results(id = "userMap",value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示实体类属性名。
            @Result(id = true,column = "user_id",property = "id"),
            @Result(column = "user_name",property = "name"),
            @Result(column = "user_password",property = "password"),
            @Result(column = "user_email",property = "email"),
            @Result(column = "user_role",property = "role"),
            @Result(column = "user_status",property = "status")
    })
    User login(User user);

    //注册时验证该邮箱是否已经被注册
    @Select("select * from user where user_email=#{email}")
    @ResultMap("userMap")
    User findUserByEmail(String email);

    //注册
    @Insert("insert into user(user_name,user_password,user_email,user_role,user_status)" +
            "values(#{name},#{password},#{email},#{role},#{status})")
    int register(User user);


    @Select({"<script>" +
            "SELECT * FROM user " +
            "where 1=1"+
            "<if test=\"name != null\"> AND  user_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"email != null\"> AND user_email like  CONCAT('%', #{email},'%') </if>" +
            "<if test=\"role != null\"> AND user_role like  CONCAT('%', #{role},'%')</if>" +
            "</script>"
    })
    @ResultMap("userMap")
        //分页查询用户信息
        Page<User> findAll(User user);
        //新增用户
        Integer addUser(User user);
        //编辑用户信息
        Integer editUser(User user);


        //通过Id查找用户信息
    @Select("select * from user where user_id=#{id}")
    @ResultMap("userMap")
    User findUserById(int id);

    //通过id删除用户信息
    @Delete("delete from user where user_id=#{id}")
    int deleteUser(int id);
}
